﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using blackhouse;

namespace blackhouse.Translator.Web {
    public static class TranslatorExtension {

        public static string GetFormattedResource(this TemplateControl control, string classKey, string resourceKey) {
            return GetFormattedResource(control, classKey, resourceKey, TranslatorFormatType.None);
        }

        public static string GetFormattedResource(this TemplateControl control, string classKey, string resourceKey, TranslatorFormatType formatType, params object[] param) {
            object objV = HttpContext.GetGlobalResourceObject(classKey, resourceKey);
            if (objV == null) return String.Empty;
            string v = objV.ToString();
            if (formatType.HasFlag(TranslatorFormatType.FormatString))
                v = String.Format(v, param);
            if (formatType.HasFlag(TranslatorFormatType.StringFormat))
                v = String.Format(param[0].ToString(), objV);
            if (formatType.HasFlag(TranslatorFormatType.AllLower))
                v = v.ToLower();
            if (formatType.HasFlag(TranslatorFormatType.AllUpper))
                v = v.ToUpper();
            if (formatType.HasFlag(TranslatorFormatType.FirstUpper))
                v = v[0].ToString().ToUpper() + v.Substring(1);
            return v;
        }

    }

    [Flags]
    public enum TranslatorFormatType {
        None = 0,
        /// <summary>
        /// Tekst małymi literami
        /// </summary>
        AllLower = 0x1,
        /// <summary>
        /// Tekst dużymi literami
        /// </summary>
        AllUpper = 0x2,
        /// <summary>
        /// Pierwsza litera duża
        /// </summary>
        FirstUpper = 0x4,
        /// <summary>
        /// Wartość jest przeformatowana przez format podany w pierwszym parametrze
        /// </summary>
        StringFormat = 0x8,
        /// <summary>
        /// Wartość z zasobu jest formatem, a w parametrach przekazywane są wartości - objekty do formatowania
        /// </summary>
        FormatString = 0x10,
        LowerWithFirstUpper = 0x5
    }

}
